package com.gking.resourcePool.service.ProjectGroup;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gking.resourcePool.common.BaseContext;
import com.gking.resourcePool.common.Result;
import com.gking.resourcePool.entity.Project;
import com.gking.resourcePool.entity.ProjectGroup;
import com.gking.resourcePool.entity.Resource;
import com.gking.resourcePool.mapper.ProjectGroupMapper;
import com.gking.resourcePool.service.Project.ProjectService;
import com.gking.resourcePool.service.Resource.ResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
@Scope(value = "prototype", proxyMode = ScopedProxyMode.TARGET_CLASS)//三级缓存解决bean循环注入问题
public class ProjectGroupServiceImpl extends ServiceImpl<ProjectGroupMapper, ProjectGroup> implements ProjectGroupService {

    @Autowired
    private ProjectService projectService;

    @Autowired
    private ResourceService resourceService;


    /**
     * 添加项目分组
     *
     * @param name        分组名称
     * @param description 分组描述
     * @param projectId   分组所属项目id
     * @return s
     */
    @Override
    public Result<String> addProjectGroup(String name, String description, String projectId) {
        LambdaQueryWrapper<Project> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Project::getId, projectId);
        wrapper.eq(Project::getUserId, BaseContext.getCurrentId());
        wrapper.eq(Project::getIsDelete, 0);
        if (projectService.getOne(wrapper) == null)
            return Result.error("项目不存在，无法添加分组");

        ProjectGroup projectGroup = new ProjectGroup();
        projectGroup.setName(name);
        projectGroup.setDescription(description);
        projectGroup.setProjectId(projectId);
        projectGroup.setUserId(BaseContext.getCurrentId());
        this.save(projectGroup);

        return Result.success("添加成功");
    }

    /**
     * 修改项目分组
     *
     * @param id          项目分组id
     * @param name        分组名称
     * @param description 分组描述
     * @return s
     */
    @Override
    public Result<String> modifyProjectGroup(String id, String name, String description) {
        LambdaUpdateWrapper<ProjectGroup> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ProjectGroup::getId, id);
        wrapper.eq(ProjectGroup::getUserId, BaseContext.getCurrentId());
        wrapper.eq(ProjectGroup::getIsDelete, 0);
        wrapper.set(ProjectGroup::getName, name);
        wrapper.set(ProjectGroup::getDescription, description);
        wrapper.set(ProjectGroup::getUpdateTime, LocalDateTime.now());
        wrapper.set(ProjectGroup::getUpdateUserId, BaseContext.getCurrentId());
        this.update(wrapper);

        return Result.success("修改成功");
    }

    /**
     * 删除项目分组
     *
     * @param id 项目id
     * @return s
     */
    @Override
    @Transactional
    public Result<String> deleteProjectGroup(String id) {
        LambdaUpdateWrapper<ProjectGroup> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(ProjectGroup::getId, id);
        wrapper.eq(ProjectGroup::getUserId, BaseContext.getCurrentId());
        wrapper.eq(ProjectGroup::getIsDelete, 0);
        wrapper.set(ProjectGroup::getIsDelete, 1);
        wrapper.set(ProjectGroup::getDeleteTime, LocalDateTime.now());
        wrapper.set(ProjectGroup::getDeleteUserId, BaseContext.getCurrentId());
        this.update(wrapper);

        LambdaUpdateWrapper<Resource> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Resource::getGroupId, id);
        updateWrapper.eq(Resource::getUserId, BaseContext.getCurrentId());
        updateWrapper.eq(Resource::getIsDelete, 0);
        updateWrapper.set(Resource::getIsDelete, 1);
        updateWrapper.set(Resource::getDeleteTime, LocalDateTime.now());
        updateWrapper.set(Resource::getDeleteUserId, BaseContext.getCurrentId());
        resourceService.update(updateWrapper);

        return Result.success("删除成功");
    }

    /**
     * 获取项目分组列表
     *
     * @param projectId 项目id
     * @return s
     */
    @Override
    public Result<List<ProjectGroup>> getProjectGroupList(String projectId) {
        LambdaQueryWrapper<Project> projectWrapper = new LambdaQueryWrapper<>();
        projectWrapper.eq(Project::getId, projectId);
        projectWrapper.eq(Project::getUserId, BaseContext.getCurrentId());
        projectWrapper.eq(Project::getIsDelete, 0);
        if (projectService.getOne(projectWrapper) == null) return Result.error("项目不存在");

        LambdaQueryWrapper<ProjectGroup> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProjectGroup::getProjectId, projectId);
        wrapper.eq(ProjectGroup::getUserId, BaseContext.getCurrentId());
        wrapper.eq(ProjectGroup::getIsDelete, 0);
        wrapper.orderByDesc(ProjectGroup::getCreateTime);
        List<ProjectGroup> list = this.list(wrapper);
        return Result.success("获取成功！", list);
    }
}
